<template>
    <div class="botton-bar">
        <div class="check-content">
            <check-button class="check-button" @click.native="totalClick" :ischeck="isSelectAll"></check-button>
            <span>全选</span>
        </div>
        <div class="totalPrice">
            <span>合计{{totalPrice}}</span>
        </div>

        <div class="totalLength" @click="saveClick">
            <span>去结账({{totalLength}})</span>
        </div>
        <toast :message="messageList" :is-show="isshow"></toast>
    </div>
</template>

<script>
    import CheckButton from './CheckButton'
    import toast from '../../../components/common/toast/toast'
    import {mapGetters} from 'vuex'

    export default {
        name: "CartBottonBar",
        components: {
            CheckButton,
            toast
        },
        data() {
            return {
                isshow: false,
                messageList: "请选择您想购买的商品！！！"
            }
        },
        computed: {
            ...mapGetters([
                'cartList'
            ]),
            totalPrice() {
                //拿到vuex中的cart购物车数组
                return '￥' + this.$store.state.cartList.filter(item => {
                    //判断item.check是否为true，true则过滤进新数组
                    return item.checked
                }).reduce((preValue, item) => {
                    //prevalue为前一个值（参数)，reduce为返回后的数组  记得尾部一定要传一个时间
                    return preValue + parseInt(item.price.substring(1)) * item.count
                }, 0)
            },

            totalLength() {
                return this.$store.state.cartList.filter(item => {
                    return item.checked
                }).length
            },
            isSelectAll() {
                //判断VUEX里的cartList内的是否都被选中
                // if购物车没有东西的时候
                if (this.cartList.length === 0) return false
                return !(this.cartList.filter(item => !item.checked).length)
            }
        },
        methods: {
            totalClick() {
                //只有两种情况，全选了 或者有部分没选中 （包含全没选）
                if (this.isSelectAll)//全部选中了   此时点击变成全不选
                {
                    this.cartList.forEach(item => {
                        item.checked = false
                    })
                } else {//有没选中的
                    this.cartList.forEach(item => {
                        item.checked = true
                    })
                }
            },
            saveClick() {
                if (this.cartList.filter(item => item.checked).length == 0) {
                    //当前没有选中任何商品
                    this.isshow = true
                    setTimeout(() => {
                        this.isshow = false
                    }, 1000)
                } else {
                    this.$emit('saveClick')
                }
            }
        },

    }
</script>

<style scoped>
    .botton-bar {
        height: 40px;
        background-color: #eee;
        position: relative;
        top: 440px;
        display: flex;

    }

    .check-content {
        line-height: 20px;
        display: flex;
        align-items: center;
        margin-left: 10px;
        width: 70px;


    }

    .totalPrice {
        padding-top: 10px;
        flex: 1;
        padding-left: 20px;
    }

    .totalLength {
        padding-top: 10px;
        width: 90px;
        background-color: #CC3399;
        color: #ffffff;
    }

    .check-button {
        width: 20px;
        height: 20px;
        margin-right: 5px;
    }
</style>